// pages/wifidemo/wifidemo.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    wifiList: [],
    myList: [],
    show:false,
    chuancan: '',
    passwd: '',
  },
  tabactive(e){
    this.setData({
      active: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index == 2){
      this.getmylist()
    }
  },
  lConnectWifi(e){
      this.setData({
        chuancan: this.data.wifiList[e.currentTarget.dataset.idx]
      })
    this.setData({
      show: !this.data.show
    })
  },
  delwifi(e){
    let idx = e.currentTarget.dataset.idx
    util.API('pyapi/add_wifi', {
        type: 'del',
        wifi_id: this.data.myList[idx].wifi_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        data,
        code
      } = res
      if (code === 200) {
        wx.showToast({
          icon: 'none',
          title: `${res.msg}`,
          duration: 3000
        })
        this.getmylist()
      } else {
        wx.showToast({
          title: `${res.msg}`,
          icon: 'none'
        })
        this.getmylist()
      }
    })

  },
  // 绑定的密码
  bindKeyInput: function (e) {
    this.setData({
      passwd: e.detail.value
    })
  },
  jiaru(){
    
  },
  addwifi(){
    if (this.data.passwd === ''){
      wx.showToast({
        icon: 'none',
        title: '请填写密码',
        duration: 3000
      })
      return false
    }
    wx.showToast({
      title: `${this.data.chuancan.BSSID}`,
      icon: 'none'
    })
    util.API('pyapi/add_wifi', {
      bssid: this.data.chuancan.BSSID,
      ssid: this.data.chuancan.SSID,
      passwd: this.data.passwd,
      wifi_name: this.data.chuancan.SSID
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        data,
        code
      } = res
      if (code === 200) {
        wx.showToast({
          icon: 'none',
          title: '添加wifi成功',
          duration: 3000
        })
        this.setData({
          show: false
        })
      } else {
        wx.showToast({
          title: '添加wifi失败',
          icon: 'none'
        })
        this.setData({
          show: false
        })
      }
    })
  },
  getmylist(){
    util.API('pyapi/wifi_list', {
      pageshow: 100
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        data,
        code
      } = res
      if (code === 200) {
        this.setData({
          myList: data
        })
        if(data.length == 0 && this.data.active == 2){
          wx.showToast({
            icon: 'none',
            title: '您没有添加过wifi',
            duration: 1000
          })
        }
      } else {

      }
    })
  },
  lGetWifiList: function () {
    wx.getWifiList({
      success: function (res) {
        console.log(`wifi权限成功了`)
        wx.showToast({
          title: res.errMsg,
        })
      },
      fail: function (res) {
        console.log(`wifi权限失败`)
        wx.showToast({
          icon: 'none',
          title: '请打开wifi再次尝试',
          duration: 3000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getmylist()
    // 获取wifi列表
    let that = this
    wx.getSystemInfo({
      success: function (data) {
        if (data.platform == 'android') {
          // 启动wifi
          wx.startWifi()
          console.log('android')
          wx.onGetWifiList(function (res) {
            console.log('获取到的wifi列表：' + res.wifiList)
            that.setData({
              wifiList: res.wifiList,
            })
          })
          that.lGetWifiList()
        } else {
          wx.startWifi({
            success: function (res) {
              wx.showModal({
                title: '提示',
                confirmColor: '#ff761e',
                content: '因苹果系统原因，苹果用户需打开设置—Wi-Fi，等待Wi-Fi读取成功，再返回小程序进行配置',
                success: function (pp) {
                  if (pp.confirm) {
                    wx.getWifiList({
                      success: function (data) {
                        console.log(`wifi权限成功了`)
                        wx.showToast({
                          title: res.errMsg,
                        })
                        wx.onGetWifiList(function (res) {
                          console.log('获取到的wifi列表：' + res)
                          that.setData({
                            wifiList: res.wifiList,
                          })
                          wx.setWifiList({
                            wifiList: res.wifiList,
                            success: function (dd) {
                              console.log(dd)
                            }
                          })
                        })
                      },
                      fail: function (data) {
                        console.log(`wifi权限失败`)
                        wx.showToast({
                          icon: 'none',
                          title: '请打开wifi再次尝试',
                          duration: 3000
                        })
                      }
                    })
                  } else if (pp.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })

            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
 
})